export default {
  namespaced: true,
  state: () => ({
    address: uni.getStorageSync('address') || {},
    token: uni.getStorageSync('token') || '',
    userInfo: uni.getStorageSync('userInfo') || {},
    redirectInfo: null
  }),
  mutations: {
    updateRedirectInfo(state, redirectInfo) {
      state.redirectInfo = redirectInfo
    },
    updateAddress(state, address) {
      state.address = address
      // 持久化到本地
      this.commit('m_user/saveAddressToStorage')
    },
    saveAddressToStorage(state) {
      uni.setStorageSync('address', state.address)
    },
    saveUserInfo(state, userInfo) {
      state.userInfo = userInfo
      // 持久化到本地
      this.commit('m_user/saveUserInfoToStorage')
    },
    saveUserInfoToStorage(state) {
      uni.setStorageSync('userInfo', state.userInfo)
    },
    updateToken(state, token) {
      state.token = token
      // 持久化 token
      this.commit('m_user/saveTokenToStorage')
    },
    saveTokenToStorage(state) {
      uni.setStorageSync('token', state.token)
    }
  },
  getters: {
    addstr(state) {
      // 判断address是否已经获取
      if (JSON.stringify(state.address) === '{}') return ''
      // 拼接 省，市，区，详细地址 的字符串并返回给用户
      return state.address.provinceName + state.address.cityName + state.address.countyName + state.address.detailInfo
    }
  }
}
